/* 
 * UrlyBirdMainWindow.java 19.06.2011
 *  
 *  
 * Candidate: Alessandra Otterbach
 * Prometric ID: Algisina1
 * Username: Algisina
 *  
 * Sun Certified Developer for Java 2 Platform, Standard Edition Programming 
 * Assignment (CX-310-252A)
 *  
 * This class is part of the Programming Assignment of the Sun Certified 
 * Developer for Java 2 Platform, Standard Edition certification program, must 
 * not be used out of this context and must be used exclusively by Sun 
 * Microsystems, Inc. 
 */
package suncertify.ps;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.rmi.registry.Registry;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

import suncertify.util.PropertiesHelper;
import suncertify.util.StringUtils;

/**
 * Main entrance Window. Filepath is chooseable.
 * 
 * @author Alessandra Otterbach
 * 
 */
public class UrlyBirdMainWindow extends JFrame {

    private ApplicationRunMode mode;

    public final int iw = 4;
    public final Insets formPanelInsets = new Insets(iw, iw, iw, iw);
    public GridBagConstraints gbc = new GridBagConstraints();

    public JButton jbtStart;
    public JButton jbtCancel;
    public JButton jbtPath;

    public JLabel jlbFileName;
    public JTextField jtxtFileName;
    private JLabel jlbPort;
    private JTextField jtxtPort;
    private JLabel jlbIp;
    private JTextField jtxtIp;

    public JPanel panelInput;
    public JPanel panelButtons;

    public JFileChooser fc;

    public UrlyBirdMainWindow() {
	super("Urly Bird");
    }

    public UrlyBirdMainWindow(ApplicationRunMode mode) {
	super("Urly Bird");
	this.mode = mode;
    }

    /**
     * Calls methods to create the gui and to initialize the window.
     */
    public void startGui() {
	createGui();
	init();
    }

    /**
     * Initializes all the default window behaviours
     * 
     * @param args
     */
    private void init() {

	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.pack();
	this.setSize(500, 180);

	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	int x = (int) ((dim.getWidth() - this.getWidth()) / 2);
	int y = (int) ((dim.getHeight() - this.getHeight()) / 2);
	this.setLocation(x, y);
	this.setResizable(false);
	this.setVisible(true);
    }

    /**
     * Create the GUI and show it. TODO For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    public void createGui() {

	Properties properties = PropertiesHelper.readProperties();

	// Layout definition
	defineLayout();
	
	// DB filename and path
	createFilePathPanel();

	// Sichtbar, wenn nicht Standalone Mode
	// TODO ausgliedern?
	if (ApplicationRunMode.NETWORK_CLIENT.equals(mode)) {

	    // Port
	    jlbPort = new JLabel("Port: ");
	    gbc.gridx = 0;
	    gbc.gridy = 1;
	    panelInput.add(jlbPort, gbc);

	    jtxtPort = new JTextField();
	    jtxtPort.setColumns(15);
	    if (!properties.isEmpty()
		    && StringUtils.isNotEmpty(properties
			    .getProperty(PropertiesHelper.PROP_PORT))) {
		jtxtPort.setText(properties
			.getProperty(PropertiesHelper.PROP_PORT));
	    } else {

		jtxtPort.setText(String.valueOf(Registry.REGISTRY_PORT));
	    }
	    gbc.gridx = 1;
	    gbc.gridy = 1;
	    panelInput.add(jtxtPort, gbc);

	    // Ip
	    jlbIp = new JLabel("IP: ");
	    gbc.gridx = 0;
	    gbc.gridy = 2;
	    panelInput.add(jlbIp, gbc);

	    jtxtIp = new JTextField();
	    jtxtIp.setColumns(15);
	    if (!properties.isEmpty()
		    && StringUtils.isNotEmpty(properties
			    .getProperty(PropertiesHelper.PROP_IP))) {
		jtxtIp.setText(properties.getProperty(PropertiesHelper.PROP_IP));
	    }
	    gbc.gridx = 1;
	    gbc.gridy = 2;
	    panelInput.add(jtxtIp, gbc);
	}

	// Buttons
	panelButtons = new JPanel();
	panelButtons.setLayout(new GridBagLayout());

	jbtStart = new JButton("Ok");
	jbtStart.addActionListener(ok);
	gbc.gridx = 0;
	gbc.gridy = 1;
	panelButtons.add(jbtStart, gbc);

	jbtCancel = new JButton("Cancel");
	// jbtCancel.addActionListener(book);
	gbc.gridx = 2;
	gbc.gridy = 1;
	panelButtons.add(jbtCancel, gbc);

	this.add(panelInput, BorderLayout.NORTH);
	this.add(panelButtons, BorderLayout.SOUTH);
    }

    public void defineLayout() {
	gbc.insets = formPanelInsets;
	gbc.fill = GridBagConstraints.BOTH;

	Border etchedBorder = new EtchedBorder();
	Border emptyBorder = new EmptyBorder(9, 9, 9, 9);
	Border panelBorder = BorderFactory.createCompoundBorder(emptyBorder,
		etchedBorder);

	// Initialize panel for input fields
	panelInput = new JPanel();
	panelInput.setLayout(new GridBagLayout());
	panelInput.setBorder(panelBorder);

    }

    public void createFilePathPanel() {
	jlbFileName = new JLabel("Filename with path: ");
	gbc.gridx = 0;
	gbc.gridy = 0;
	panelInput.add(jlbFileName, gbc);

	jtxtFileName = new JTextField();
	jtxtFileName.setColumns(15);
	jtxtFileName.setText(GuiHelper.getPropertyPath());

	gbc.gridx = 1;
	gbc.gridy = 0;
	panelInput.add(jtxtFileName, gbc);

	jbtPath = new JButton("Choose File");
	jbtPath.addActionListener(fileChooser);
	gbc.gridx = 2;
	gbc.gridy = 0;
	panelInput.add(jbtPath, gbc);
    }

    ActionListener ok = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	    System.out.println("Main");

	    dispose();

	    if (ApplicationRunMode.NETWORK_CLIENT.equals(mode)) {
		// Save Properties
		PropertiesHelper.storePropertiesNetwork(jtxtFileName.getText(),
			jtxtIp.getText(), jtxtPort.getText());
		new BookingView(jtxtFileName.getText(), jtxtIp.getText(),
			jtxtPort.getText(), mode);
	    } else {
		// Save Properties
		PropertiesHelper.storePropertiesStandalone(jtxtFileName
			.getText());
		BookingView view = new BookingView(jtxtFileName.getText(), mode);
	    }
	}
    };

    ActionListener fileChooser = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	    fc = new JFileChooser();
	    // fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	    if (StringUtils.isNotEmpty(jtxtFileName.getText())) {
		fc.setSelectedFile(new File(jtxtFileName.getText()));
	    }
	    int state = fc.showOpenDialog(null);
	    if (JFileChooser.APPROVE_OPTION == state) {
		jtxtFileName.setText(fc.getSelectedFile().getAbsolutePath());
	    }
	}
    };

    public static void main(final String[] args) {
	UrlyBirdMainWindow view = new UrlyBirdMainWindow(
		ApplicationRunMode.STANDALONE_CLIENT);
    }
}
